<?php
declare (strict_types=1);

namespace addons\V2\admin\controller;

use addons\Builder\facade\Builder;
use app\common\model\Area;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;

class Order extends \app\admin\controller\Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\addons\V2\admin\model\Order';

    //关联模型
    protected $hasOne = ['user'];

    protected int $takeOver = 3;
    protected array $append = [];

    public function index()
    {
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'title' => '订单ID', 'width' => 90])
            ->addColumn(['field' => 'sn', 'title' => '订单编号', 'width' => 170])
            ->addColumn(['field' => 'product', 'title' => '订单商品', 'width' => 350, 'templet' => '<div>{{- d.product}}</div>'])
            ->addColumn(['field' => 'total_amount', 'title' => '订单价格'])
            ->addColumn(['field' => 'real_amount', 'title' => '实际支付'])
            ->addColumn(['field' => 'code', 'title' => '下单会员', 'templet' => '#_userTpl', 'width' => 200])
            ->addColumn(['field' => 'create_time', 'title' => '下单时间', 'width' => 150])
            ->addColumn(['field' => 'name', 'title' => '姓名'])
            ->addColumn(['field' => 'phone', 'title' => '手机号', 'width' => 100])
            ->addColumn(['field' => 'address_details', 'title' => '我的位置'])
            ->addColumn(['field' => 'start_address', 'title' => '起始地', 'width' => 150])
            ->addColumn(['field' => 'end_address', 'title' => '目的地', 'width' => 120])
            ->addColumn(['field' => 'subscribe_time', 'title' => '预约时间', 'width' => 150])
            ->addColumn(['field' => 'status', 'title' => '订单状态'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#orderDemo', 'title' => '操作', 'width' => 290])
            ->addTopButtons(['deleteAll', 'export'])
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['sn', '订单编号', 'input'],
                ['status', '订单状态', 'select', $this->model::getOrderStatus('', 2)]
            ])
            ->addRightButtons([
                'edit' => [
                    'title' => '详情',
                    'unity_title' => '订单详情',
                    'size' => '50%',
                    'site' => 'r'
                ],
                'payment' => ['title' => '已付款',
                    'url' => url('Order/reset')->build(),
                    'event' => 'refund',
                ],
                'finish' => ['title' => '已完成',
                    'url' => url('Order/reset')->build(),
                    'event' => 'refund',
                ],
                'after' => ['title' => '售后中',
                    'url' => url('Order/reset')->build(),
                    'event' => 'refund',
                ],
                'delete',

            ])
            ->addJsUrl(['order'])
            ->addExtraHtml(['order'])
            ->fetch();
    }

    public function edit($id = '', $tpl = '', $info = '')
    {
        return parent::edit($id, $this->app->getRootPath() . 'addons/V2/admin/view/order/edit.html', $info); // TODO: Change the autogenerated stub
    }

    public function reset()
    {
        $orderId = $this->request->param('id');
        return Result::fastReturn($this->model::resetStatus($orderId, $this->admin));
    }

    public function deliver($id = '')
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                $this->validate($param, '\addons\V2\admin\validate\OrderDeliver');
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            return json($this->model::deliver($param, $this->admin));
        }

        $info = $this->model::find($id);

        View::assign([
            'express' => \app\common\model\Express::where(['status' => 1])->select(),
            'info' => $info
        ]);
        return View::fetch($this->app->getRootPath() . 'addons/V2/admin/view/order/deliver.html');
    }

    public function getArea($id, $level = 0)
    {
        return Area::getArea($id ?: 100000, $level);
    }


}
